package org.example.linkedList;

/**
 * @Auther: wangbw
 * @Date:2020/12/2
 * @Description: org.example.linkedList
 * @version: 1.0
 */
public class DelNode {
    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        Node head = Node.createNode(10);
        Node.print(head);
        Node.print(delNode(head, 3));
    }

    //快慢， 1-2-5-6； 2； 1-2-6
    private static Node delNode(Node head, int back) {
        Node cur = head;
        Node slow = head;
        Node pre = null;
        while (cur != null){
            if (back == 0){
                break;
            }
            cur = cur.next;
            back --;
        }
        if (cur == null){
            return head.next;
        }
        while (cur != null){
            pre = slow;
            cur = cur.next;
            slow = slow.next;
        }
        pre.next = slow.next;
        return head;
    }
}
